package com.qipay.sp.excutor.zhangxin;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.order.config.OrderStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.DateUtils;
import com.qipay.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class ZxAliNativeExcutor extends AbstractExcutor {

    private final static String API_URL="api_url";
    @Autowired
    OrderService orderService;


    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.WX_NATIVE;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("amount", new BigDecimal(order.getAmount()).divide(new BigDecimal("100"),2,BigDecimal.ROUND_DOWN));
        apiParams.put("currency","156");
        apiParams.put("order_no", order.getTradeNo());
        apiParams.put("pay_code","20000");//20000:支付宝扫码
        apiParams.put("pay_ip",order.getRemark());
        apiParams.put("request_time", DateUtils.sdf.format(order.getCreateTime()));
        apiParams.put("product_name",order.getBody());
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {
        Object apiUrl = getVal(spconfig, API_URL);
        Object merchantNo= getVal(spconfig, "merchant_no");
        Object key= getVal(spconfig, "key");
        Object notifyUrl= getVal(spconfig, "notify_url");
        Object returnUrl= getVal(spconfig, "return_url");
        params.put("merchant_no",merchantNo);
        params.put("notify_url",notifyUrl);
        params.put("return_url",returnUrl);
        String sign = MD5Util.getSign(params,key.toString());
        params.put("sign",sign);

        String result = sendPostByKeyValue(params,apiUrl.toString());

        if (StringUtils.isBlank(result)) {
            throw new Exception("请求掌心付的支付宝接口失败");
        }

        Map<String, Object> responseMap = JSONObject.parseObject(result);
        Map<String, Object> resultMap = new HashMap<>();

        //储存接口请求返回的
        Order order = orderService.getByTradeNo(params.get("order_no").toString());
//        order.setRemark(result);

        if (responseMap == null || !"0000".equals(responseMap.get("resp_code")==null?"":responseMap.get("resp_code").toString()) ) {
            order.setState(OrderStateEnum.REQERROR.getType());
            orderService.update(order);
            logger.error("请求掌心付的支付宝接口失败:" + result);
            resultMap.put("result_code", "1");
            resultMap.put("message", responseMap.get("resp_msg"));
            return resultMap;
        }

        order.setState(OrderStateEnum.WAITING.getType());
        orderService.update(order);

        resultMap.put("result_code", "0");
        resultMap.put("code_url", responseMap.get("data"));
        return resultMap;
    }
}
